mixin getLink(path, name, metric, type)
  - isSet = false
  if options.browsertime.iterations % 2 === 0 && type === 'median'
    td.number(data-title=type) #{metric[type]}
  else
    each run, index in path
      if (run[name] === metric[type] && isSet === false)
        - isSet = true
        td.number(data-title=type)
          a(href='./' + (index+1) + '.html', title='Go to the run with the ' + type + ' ' + name) #{h.time.ms(metric[type])}

mixin getLinkTimings(path, name, metric, type)
  - isSet = false
  if options.browsertime.iterations % 2 === 0 && type === 'median'
    td.number(data-title=type) #{metric[name][type]}
  else
    each run, index in path
      if (Number(run.timings[name]).toFixed(0) === Number(metric[name][type]).toFixed(0) && isSet === false)
        - isSet = true
        td.number(data-title=type)
          a(href='./' + (index+1) + '.html', title='Go to the run with the ' + type + ' ' + name) #{h.time.ms(metric[name][type])}

mixin getLinkFirstInput(path, name, metric, type)
  - isSet = false
  if options.browsertime.iterations % 2 === 0 && type === 'median'
    td.number(data-title=type) #{metric[name][type]}
  else
    each run, index in path
      if (Number(run.timings.firstInput[name]).toFixed(0) === Number(metric[name][type]).toFixed(0) && isSet === false)
        - isSet = true
        td.number(data-title=type)
          a(href='./' + (index+1) + '.html', title='Go to the run with the ' + type + ' ' + name) #{h.time.ms(metric[name][type])}

mixin getLinkPageTimings(path, name, metric, type)
  - isSet = false
  if options.browsertime.iterations % 2 === 0 && type === 'median'
    td.number(data-title=type) #{metric[name][type]}
  else
    each run, index in path
      if (Number(run.timings.pageTimings[name]).toFixed(0) === Number(metric[name][type]).toFixed(0) && isSet === false)
        - isSet = true
        td.number(data-title=type)
          a(href='./' + (index+1) + '.html', title='Go to the run with the ' + type + ' ' + name) #{h.time.ms(metric[name][type])}

mixin getUserTimings(path, name, metric, type)
  - isSet = false
  if options.browsertime.iterations % 2 === 0 && type === 'median'
    td.number(data-title=type) #{metric[type]}
  else
    each run, index in path
      each marks in run.timings.userTimings.marks
          if (marks.name === name && Number(marks.startTime).toFixed(0) === Number(metric[type]).toFixed(0) && isSet === false)
            - isSet = true
            td.number(data-title=type)
              a(href='./' + (index+1) + '.html', title='Go to the run with the ' + type + ' ' + name) #{h.time.ms(metric[type])}

- btStatistics = h.get(pageInfo.data, 'browsertime.pageSummary.statistics')

if btStatistics
  h2 Timings Summary
  - timingMetrics = ['firstPaint', 'rumSpeedIndex']
  .responsive
    table
      thead
        tr
          th Metric
          th.number min
          th.number median
          th.number mean
          th.number max
      if btStatistics.visualMetrics
        tr 
          td.extraheader(colspan='5') Visual Metrics
        each visualMetric, name in btStatistics.visualMetrics
          tr
            td(data-title='metric') #{name}
            +getLink(pageInfo.data.browsertime.pageSummary.visualMetrics, name, visualMetric, 'min')
            +getLink(pageInfo.data.browsertime.pageSummary.visualMetrics, name, visualMetric, 'median')
            td.number(data-title='mean') #{h.time.ms(visualMetric.mean)}
            +getLink(pageInfo.data.browsertime.pageSummary.visualMetrics, name, visualMetric, 'max')
      if btStatistics.timings && btStatistics.timings.pageTimings
        tr 
          td.extraheader(colspan='5') RUM Timing Metrics
        tr
          td(data-title='metric') backEndTime
          +getLinkPageTimings(pageInfo.data.browsertime.pageSummary.browserScripts, 'backEndTime', btStatistics.timings.pageTimings, 'min')
          +getLinkPageTimings(pageInfo.data.browsertime.pageSummary.browserScripts, 'backEndTime', btStatistics.timings.pageTimings, 'median')
          td.number(data-title='mean') #{h.time.ms(btStatistics.timings.pageTimings.backEndTime.mean)}
          +getLinkPageTimings(pageInfo.data.browsertime.pageSummary.browserScripts, 'backEndTime', btStatistics.timings.pageTimings, 'max')

        each timing in timingMetrics
          if btStatistics.timings[timing]
            tr
              td(data-title='metric') #{timing}
              +getLinkTimings(pageInfo.data.browsertime.pageSummary.browserScripts, timing, btStatistics.timings, 'min')
              +getLinkTimings(pageInfo.data.browsertime.pageSummary.browserScripts, timing, btStatistics.timings, 'median')
              td.number(data-title='mean') #{h.time.ms(btStatistics.timings[timing].mean)}
              +getLinkTimings(pageInfo.data.browsertime.pageSummary.browserScripts, timing, btStatistics.timings, 'max')
      if btStatistics.timings && btStatistics.timings.userTimings
        if btStatistics.timings.userTimings.marks
          tr 
            td.extraheader(colspan='5') User Timing 
          each userTimings, name in btStatistics.timings.userTimings.marks
            tr
              td(data-title= 'User Timing') #{name}
              +getUserTimings(pageInfo.data.browsertime.pageSummary.browserScripts, name, userTimings, 'min')
              +getUserTimings(pageInfo.data.browsertime.pageSummary.browserScripts, name, userTimings, 'median')
              td.number(data-title='mean') #{userTimings ? h.time.ms(userTimings.mean): ''}
              +getUserTimings(pageInfo.data.browsertime.pageSummary.browserScripts, name, userTimings, 'max')
      if btStatistics.timings && btStatistics.timings.firstInput
        if btStatistics.timings.firstInput.duration
          tr
            td.extraheader(colspan='5') First Input Delay
          tr
            td(data-title= 'First Input Delay') Duration
            +getLinkFirstInput(pageInfo.data.browsertime.pageSummary.browserScripts, 'duration', btStatistics.timings.firstInput, 'min')
            +getLinkFirstInput(pageInfo.data.browsertime.pageSummary.browserScripts, 'duration', btStatistics.timings.firstInput, 'median')
            td.number(data-title='mean') #{h.time.ms(btStatistics.timings.firstInput.duration.mean)}
            +getLinkFirstInput(pageInfo.data.browsertime.pageSummary.browserScripts, 'duration', btStatistics.timings.firstInput, 'max')
